// 分页查询


function displayPage(pageLen){
    let pageNum = parseInt(pageLen/10+1);
    console.log(pageNum);
    let pageBtnBox = document.getElementsByClassName("pageBox")[0];
    
    for(let i = 0 ; i < pageNum ; i++){
    let str =  ` <button type="button" class="pageBtn nomal">${i+1}</button>`
    pageBtnBox.innerHTML += str;
    }
    
    let pageBtn = document.querySelectorAll(".pageBtn");
    for(let i = 0 ; i < pageNum ; i++){
        pageBtn[i].onclick = function(){
           
            for(let j = 0 ; j < pageNum ;j++){
                pageBtn[j].setAttribute("class","pageBtn nomal");
            }
            pageBtn[i].setAttribute("class","pageBtn actived");
            dyGetForm(`dynamicState/select/page/${i+1}/10`).then(value=>{
                if(value.code == 200){
                    console.log(value.msg);
              
                    //  传输响应结果同时
                    console.log(value)

                    insertAllDyPageHtml(value.data.records);

                    let detailPgaeDyBtn = document.getElementsByClassName("detailDy");
        
                    toDetailPageDy(detailPgaeDyBtn,value.data.records,i);
                   
                                        
                    if(isNight){
                        nightBtn.click();
                      }
                      else{
                        sunBtn.click();
                      }
                                        
                 }
                 else if(value.code == 11000){
                     console.log(value.msg);
                 }   
                 else{
                     alert("可能后台炸了");
                 }


            },
            reason=>{
                alert("AJAX发送请求失败");
            })


        }

    }
    

}

function toDetailPageDy(detailBtn,data,page){

    for(let i = 0 ; i < data.length ; i++){
        detailBtn[i].onclick = function(){

            let str = htmlDyDetail(data[i]);

            AllDyBox.innerHTML = str;

            getCommentText(data[i].id,data[i].userId,page);
            

        }
    }

}


function htmlDyPageTop(data){
    var str = '';
    str = ` <div class="everyDyBox">
          <div class="Dycover">
            <h3 class="detailDy">${data.title}</h3>
            <li class="DyCoverContent">${data.content}</li>
            <ul>
              <li class="DyCoverName"> ${data.username}</li>
              <li class="userDyTime">发布时间:${switchTimeFormat(data.creationTime)}</li>  
            </ul>
          </div>        
        </div>
        `;
      return str;
}


function insertAllDyPageHtml(dyArray){
    let dyAllContent = "";
    console.log(dyArray)
    for(let i = 0 ; i < dyArray.length ; i ++){
        dyAllContent += htmlDyPageTop(dyArray[i]);
    }
    //插入所有动态小卡片到容器中
    AllDyBox.innerHTML = dyAllContent;
    
    if(isNight){
        nightBtn.click();
      }
      else{
        sunBtn.click();
      }
} 